
namespace apeng {

    /**异步计数 */
    export class LoadDir {

        public count: number = 0
        public onFinish: () => void = null!
        public onSub: (data: any) => void = null!
        public logPrefix: string = ""

        constructor(count: number, onFinish: () => void, onSub?: (data: any) => void, logPrefix: string = "") {
            this.count = count
            this.onSub = onSub!
            this.onFinish = onFinish!
            this.logPrefix = logPrefix
        }

        public subCount(data?: any) {
            return () => {
                this.count--
                if (this.count < 0) {
                    console.error("引用计数错误")
                    // debugger
                }

                if (this.logPrefix)
                    console.log("引用计数:" + this.logPrefix + "-" + this.count)
                if (this.onSub)
                    this.onSub(data)
                if (this.count <= 0)
                    if (this.onFinish)
                        this.onFinish()
            }
        }


    }
}